package com.kangaroo.myhome.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.kangaroo.myhome.mapper.TSUserloginInfoMapper;
import com.kangaroo.myhome.model.*;
import com.kangaroo.myhome.utils.SecurityUtils;
import com.kangaroo.myhome.vo.Option;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kangaroo.myhome.mapper.TSysFileMapper;
import com.kangaroo.myhome.vo.ResponseModel;
import tk.mybatis.mapper.util.StringUtil;

@Service
public class UserLoginInfoService {

    @Autowired
    private TSUserloginInfoMapper tSUserloginInfoMapper;

    /**
     *
     * @param workNo
     * @param ip
     * @param page
     * @param rows
     * @return
     */
    public ResponseModel findAll(String workNo, String ip, int page, int rows) {
        ResponseModel model = new ResponseModel();

        CommonSearch search = new CommonSearch();
        search.setString1(workNo);
        search.setString2(ip);
        search.setPagination(page, rows);

        List<TSUserloginInfo> users = tSUserloginInfoMapper.selectByPagination(search);
        long count = tSUserloginInfoMapper.selectByPaginationCount(search);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }

    /**
     * @param tSUserloginInfo
     * @return
     */
    public ResponseModel saveLoginInfo(TSUserloginInfo tSUserloginInfo) {

        ResponseModel model = new ResponseModel();

        if (tSUserloginInfo == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }
        tSUserloginInfo.setId(UUID.randomUUID().toString());
        tSUserloginInfo.setLastLoginTime(new Date());
        tSUserloginInfoMapper.insertSelective(tSUserloginInfo);

        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }

    /**
     * @param tSUserloginInfo
     * @return
     */
    public ResponseModel updateLoginOutInfo(TSUserloginInfo tSUserloginInfo) {

        ResponseModel model = new ResponseModel();

        if (tSUserloginInfo == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }

        tSUserloginInfo.setLastLogoutTime(new Date());
        tSUserloginInfoMapper.updateByLastLogoutTime(tSUserloginInfo);

        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }


}
